<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 后台线路订单管理控制器
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午9:18
 */
class Order_Index_module extends CI_Module
{

    /**
     * 站点配置ID
     * @var string
     */
    private $sid = '';

    /**
     * 构造函数
     */
    function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Ordergodateprice_model');
        $this->load->model('order.Ordergodate_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Orderticket_model');
        $this->load->model('order.Orderflight_model');
        $this->load->model('order.Orderrent_model');
        $this->load->model('ticket.ticket_model');
        $this->load->model('scenic.scenic_model');
       
       
        
        $this->load->model('line.Goods_model');
        $this->load->model('member.User_model');
        $this->load->model('admin.area_model');
        $this->load->model('order.Ordergoods_model');
        $this->load->model('order.Orderdistribution_model');
        $this->load->model('flight.flight_model');
        $this->load->model('rent.rent_model');
        $this->load->model('line.Line_fx_go_price_model');
        $this->load->model('api.Java_Api_model');
    }

    /**
     * 线路订单列表
     */
    public function line_list()
    {
        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/line_list")));
        }

        $data['index_url'] = site_url('order/index/line_list');

        //查询条件
        $condition['order_sn'] = addslashes($this->input->get_post('order_sn'));
        $condition['contact'] = addslashes($this->input->get_post('contact'));
        $condition['line'] = addslashes($this->input->get_post('line'));
        $condition['go_date'] = addslashes($this->input->get_post('go_date'));
        $condition['pay'] = addslashes($this->input->get_post('pay'));
        $order_info_table = $this->db->dbprefix("order_info");
        $where = "" . $order_info_table . ".order_type=1  AND $order_info_table.user_id='" . $user_id . "' AND $order_info_table.sid=".SITEID . ' ';//线路订单

        //订单流水号
        if ($condition['order_sn']) {
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".order_sn like '%" . $condition['order_sn'] . "%' ";
        }

        if($condition['pay']==3){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status=3";
        }else if($condition['pay']==1){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status!=3";
        }

        //联系人
        if ($condition['contact']) {
            $contact_table = $this->db->dbprefix("order_contacts");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $contact_table . ".name like '%" . $condition['contact'] . "%' OR " . $contact_table . ".phone_num like '%" . $condition['contact'] . "%')";
        }

        //线路编号-名称
        if ($condition['line']) {
            $go_date_table = $this->db->dbprefix("order_go_date");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $go_date_table . ".line_name like '%" . $condition['line'] . "%')";
        }

        //团期
        if ($condition['go_date']) {
            $go_date_table = $this->db->dbprefix("order_go_date");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $go_date_table . ".go_date='" . $condition['go_date'] . "')";
        }

        $page_config['perpage'] = 20;   //每页条数
        $page_config['part'] = 2;//当前页前后链接数量
        $page_config['url'] = 'order/index/line_list';//url
        $page_config['param'] = '?order_sn=' . $condition['order_sn'] . '&contact=' . $condition['contact'] . '&line=' . $condition['line'] . '&go_date=' . $condition['go_date'];//参数
        $page_config['seg'] = 4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']) : 1;//当前页
        $page_config['total'] = $this->Orderinfo_model->get_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $result = $this->Orderinfo_model->get_index_line_list($page_config['perpage'], $page_config['nowindex'], $where);
        foreach ($result as $item) {
            $line_ids[] = $item['line_id'];
        }
        if($line_ids){
            $data['line_thumbs'] = $this->Line_model->get_thumb_by_ids($line_ids);
        }
        $data['list'] = $result;
        $data['condition'] = $condition;
        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'line')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );

        $data['params'] = array(
            'seo'=>array(
                'title'=>'我的订单',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'order'),
        );
        $data['bread'] = '我的订单' ;
        $data['extra'] = array(array(
            'nav'=>array('left'=>'order')
        ));

        $data['cat_type'] = array(
            1 => '门市订单',
            2 => '网上订单',
            3 => '掌驴宝订单',
            4 => '团购订单',
            6 => '手机订单',
            8 => '分销商订单',
            10 => '同业订单',
        );
        $this->load->view(TEMPLATE_NAME . '/line_list', $data);
    }


    /**
     * 获取线路订单详细
     */
    public function line_info()
    {
        $order_id = $this->input->get('id');

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/line_info?id=" . $order_id)));
        }

        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id, 'user_id' => $user_id, 'order_type' => 1));
        if (empty($order_row)) {
            $this->error('订单信息错误!', site_url("order/index/line_list"), '1');
        }

        //联系人信息
        $contact_row = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));

        //游客列表信息
        $tourist_row = $this->Ordertourist_model->get_list(array('order_id' => $order_id));

        //订单团期基本信息
        $go_date = $this->Ordergodate_model->get_row(array('order_id' => $order_id));

        //团期价格体系信息 已选择的

        if ($order_row['from_sid'] != $order_row['sid']&&$order_row['from_sid']!=0) {
            $price_cate = array('0' => array('price_name' => '成人', 'price' => $go_date['ws_price'], 'num' => $go_date['num']), '1' => array('price_name' => '儿童', 'price' => $go_date['cws_price'], 'num' => $go_date['cnum']));
        } else {
            $price_cate = $this->Ordergodateprice_model->get_list(array('order_id' => $order_id));
        }
//print_r($order_row);

        $data['contact_info'] = $contact_row;
        $data['tourist_info'] = $tourist_row;
        $data['order_info'] = $order_row;
        $data['go_date_info'] = $go_date;
        $data['price_list'] = $price_cate;
        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'line')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );
        $data['ajax_order_status_url'] = site_url('order/index/ajax_order_status?order_id=' . $order_id);
        $this->load->view(TEMPLATE_NAME . '/line_info', $data);
    }

    /**
     * 线路订单列表
     */
    public function flight_list()
    {
        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/line_list")));
        }


        $data['index_url'] = site_url('order/index/flight_list');

        //查询条件
        $condition['order_sn'] = addslashes($this->input->get_post('order_sn'));
        $condition['contact'] = addslashes($this->input->get_post('contact'));
        $condition['flight'] = addslashes($this->input->get_post('flight'));
        $condition['date'] = addslashes($this->input->get_post('date'));
        $condition['pay'] = addslashes($this->input->get_post('pay'));
        $order_info_table = $this->db->dbprefix("order_info");
        $where = "" . $order_info_table . ".order_type=4 AND $order_info_table.user_id='" . $user_id . "'";//线路订单


        if($condition['pay']==3){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status=3";
        }else if($condition['pay']==1){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status!=3";
        }
        //订单流水号
        if ($condition['order_sn']) {
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".order_sn like '%" . $condition['order_sn'] . "%' ";
        }

        //联系人
        if ($condition['contact']) {
            $contact_table = $this->db->dbprefix("order_contacts");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $contact_table . ".name like '%" . $condition['contact'] . "%' OR " . $contact_table . ".phone_num like '%" . $condition['contact'] . "%')";
        }

        //线路编号-名称
        if ($condition['flight']) {
            $flight_table = $this->db->dbprefix("order_flight");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $flight_table . ".line_name like '%" . $condition['flight'] . "%')";
        }

        //团期
        if ($condition['date']) {
            $flight_table = $this->db->dbprefix("order_flight");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $flight_table . ".date='" . $condition['date'] . "')";
        }

        $page_config['perpage'] = 20;   //每页条数
        $page_config['part'] = 2;//当前页前后链接数量
        $page_config['url'] = 'order/index/line_list';//url
        $page_config['param'] = '?order_sn=' . $condition['order_sn'] . '&contact=' . $condition['contact'] . '&flight=' . $condition['flight'] . '&date=' . $condition['date'];//参数
        $page_config['seg'] = 4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']) : 1;//当前页
        $page_config['total'] = $this->Orderinfo_model->get_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $result = $this->Orderinfo_model->get_index_flight_list($page_config['perpage'], $page_config['nowindex'], $where);
        $data['list'] = $result;
        $data['condition'] = $condition;
        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'jipiao')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );

        $this->load->view(TEMPLATE_NAME . '/flight_list', $data);
    }

    /**
     * 获取机票订单详细
     */
    public function flight_info()
    {
        $order_id = $this->input->get('id');

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/line_info?id=" . $order_id)));
        }

        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id, 'user_id' => $user_id, 'order_type' => 4));
        if (empty($order_row)) {
            $this->error('订单信息错误!', site_url("order/index/flight_list"), '1');
        }

        //联系人信息
        $contact_row = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));

        //游客列表信息
        $tourist_row = $this->Ordertourist_model->get_list(array('order_id' => $order_id));

        //订单团期基本信息
        $flight_data = $this->Orderflight_model->get_row(array('order_id' => $order_id));
        $flight_data['unit_price'] = ceil($flight_data['price'] - (($flight_data['price'] * $flight_data['grow_discount']) / 100));
        $data['contact_info'] = $contact_row;
        $data['tourist_info'] = $tourist_row;
        $data['order_info'] = $order_row;
        $data['flight_info'] = $flight_data;

        if ($order_row['order_status'] != '3') {
            $data['flight_server_info'] = $this->update_flight_info($order_id);     // 接口订单信息


            // 发短信
            if ($data['flight_server_info']['Status'] == '2' && !($flight_data['send_sms'])) {
                $pay_template = $GLOBALS['config']['flight_order_template_pay'];
                $target = array(
                    '{$pay_money}',
                    '{$date}',
                    '{$scityname}',
                    '{$ecityname}',
                    '{$flight}',
                    '{$stime}',
                    '{$etime}',
                    '{$pname}',
                    '{$air}'
                );

                $replace = array(
                    $flight_data['pay_money'],
                    $flight_data['date'],
                    $flight_data['scityname'],
                    $flight_data['ecityname'],
                    $flight_data['flight'],
                    date('H:i', strtotime($flight_data['stime'])),
                    date('H:i', strtotime($flight_data['etime'])),
                    $flight_data['pname'],
                    airline_word_to_name($flight_data['flight'])
                );
                $content = str_replace($target, $replace, $pay_template);
                $send_result = $this->User_model->_send_sms($contact_row['phone_num'], $content);
                if ($send_result) {
                    $this->Orderflight_model->edit_row(array('send_sms' => '1'), array('order_id' => $order_id));
                }

            }
            // 获取车票退废详情
            $refund_info = $this->flight_model->_refund_ticket_detail(array('order_sn' => $order_row['order_sn']));
            if (is_array($refund_info)) {
                if (isset($refund_info['Response']) && !empty($refund_info['Response'])) {
                    $data['refund_info'] = $refund_info['Response'];
                }
            }

        }

        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'jipiao')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );
        $data['ajax_order_status_url'] = site_url('flight/index/ajax_order_status?order_id=' . $order_id . '&pnr=' . $flight_data['pnr'] . '&order_sn=' . $order_row['order_sn']);
        $data['ajax_refund_order_url'] = site_url('flight/index/ajax_refund_order_initialize?order_id=' . $order_id . '&pnr=' . $flight_data['pnr'] . '&order_sn=' . $order_row['order_sn']);
        $this->load->view(TEMPLATE_NAME . '/flight_info', $data);
    }


    /**
     * 门票订单列表
     */
    public function ticket_list()
    {

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/ticket_list")));
        }

        $data['index_url'] = site_url('order/index/ticket_list');

        //查询条件
        $condition['order_sn'] = addslashes($this->input->get_post('order_sn'));
        $condition['contact'] = addslashes($this->input->get_post('contact'));
        $order_info_table = $this->db->dbprefix("order_info");
        $where = "" . $order_info_table . ".order_type=2  AND $order_info_table.user_id='" . $user_id . "' AND $order_info_table.sid=".SITEID .' ';//线路订单
        if($_GET['pay']==3){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status=3 ";
        }else if($_GET['pay']==1){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status!=3 ";
        }
        //订单流水号
        if ($condition['order_sn']) {
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".order_sn like '%" . $condition['order_sn'] . "%' ";
        }

        //联系人
        if ($condition['contact']) {
            $contact_table = $this->db->dbprefix("order_contacts");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $contact_table . ".name like '%" . $condition['contact'] . "%' OR " . $contact_table . ".phone_num like '%" . $condition['contact'] . "%')";
        }

        $page_config['perpage'] = 20;   //每页条数
        $page_config['part'] = 2;//当前页前后链接数量
        $page_config['url'] = 'order/index/ticket_list';//url
        $page_config['param'] = '?pay='.$_GET['pay'].'&order_sn=' . $condition['order_sn'] . '&contact=' . $condition['contact'];//参数
        $page_config['seg'] = 4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']) : 1;//当前页
        $page_config['total'] = $this->Orderinfo_model->get_ticket_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $result = $this->Orderinfo_model->get_index_ticket_list($page_config['perpage'], $page_config['nowindex'], $where);
        $data['list'] = $result;
        $data['condition'] = $condition;
        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'ticket')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );
        $this->load->view(TEMPLATE_NAME . '/ticket_list', $data);
    }

    /**
     * 门票订单详细
     */
    public function ticket_info()
    {

        $order_id = $this->input->get('id');

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/ticket_info?id=" . $order_id)));
        }
        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id, 'user_id' => $user_id, 'order_type' => 2));
        if (empty($order_row)) {
            $this->error('订单信息错误!', site_url("order/index/ticket_list"), '1');
        }

        //联系人信息
        $contact_row = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));

        //门票信息
        $table = $this->db->dbprefix("order_tickets");
        $ticket_info = $this->Orderticket_model->get_row(array($table . '.order_id' => $order_id));
        $data = array(
            'contact_info' => $contact_row,
            'order_info' => $order_row,
            'ticket_info' => $ticket_info,
            'category' => array(
                1 => '成人票',
                2 => '儿童票',
                3 => '老年票',
                4 => '家庭票',
                5 => '亲子票',
                6 => '特惠票',
            ),
            'basic_type' => array('套票', '单票'),
        );
        $data['extra'] = array(array('nav' => array('left' => 'order', 'order' => 'ticket')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['line_name'], 'keywords' => $data['row']['line_name'], 'desc' => $data['row']['line_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['line_name'] . '旅游线路预订，价格优惠。' . SITECITYNAME . '市旅游局'),
            'nav' => array('current' => 'center'),
        );
        $data['ajax_order_status_url'] = site_url('order/index/ajax_order_status?order_id=' . $order_id);
        $this->load->view(TEMPLATE_NAME . '/ticket_info', $data);
    }

    /**
     * 特色商品订单列表
     */
    public function goods_list()
    {

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/goods_list")));
        }

        $data['index_url'] = site_url('order/index/goods_list');

        //查询条件
        $condition['order_sn'] = addslashes($this->input->get_post('order_sn'));
        $condition['contact'] = addslashes($this->input->get_post('contact'));
        $condition['pay'] = addslashes($this->input->get_post('pay'));
        $order_info_table = $this->db->dbprefix("order_info");
        $where = "" . $order_info_table . ".order_type=3  AND $order_info_table.user_id='" . $user_id . "' AND $order_info_table.sid=".SITEID .' ';//线路订单

        if($condition['pay']==3){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status=3";
        }else if($condition['pay']==1){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status!=3";
        }

        //订单流水号
        if ($condition['order_sn']) {
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".order_sn like '%" . $condition['order_sn'] . "%' ";
        }

        //联系人
        if ($condition['contact']) {
            $contact_table = $this->db->dbprefix("order_contacts");
            if ($where != '') $where .= " AND ";
            $where .= " (" . $contact_table . ".name like '%" . $condition['contact'] . "%' OR " . $contact_table . ".phone_num like '%" . $condition['contact'] . "%')";
        }

        $page_config['perpage'] = 20;   //每页条数
        $page_config['part'] = 2;//当前页前后链接数量
        $page_config['url'] = 'order/index/goods_list';//url
        $page_config['param'] = '?order_sn=' . $condition['order_sn'] . '&contact=' . $condition['contact'];//参数
        $page_config['seg'] = 4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']) : 1;//当前页
        $page_config['total'] = $this->Orderinfo_model->get_goods_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $result = $this->Orderinfo_model->get_index_goods_list($page_config['perpage'], $page_config['nowindex'], $where);
        $data['list'] = $result;
        $data['condition'] = $condition;
        $data['extra'] = array(array('nav' => array('left' => 'order', 'current' => 'center', 'order' => 'goods')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['goods_name'], 'keywords' => $data['row']['goods_name'], 'desc' => $data['row']['goods_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['goods_name'] . '，价格优惠。' . SITECITYNAME . '旅游网'),
            'nav' => array('current' => 'center'),
        );
        $this->load->view(TEMPLATE_NAME . '/goods_list', $data);
    }

    /**
     * 特色商品订单详细
     */
    public function goods_info()
    {

        $order_id = $this->input->get('id');

        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        if ($user_id < 1) {
            redirect('denglu?referer=' . urlencode(site_url("order/index/goods_info?id=" . $order_id)));
        }
        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id, 'user_id' => $user_id, 'order_type' => 3));
        if (empty($order_row)) {
            $this->error('订单信息错误!', site_url("order/index/goods_list"), '1');
        }

        //联系人信息
        $contact_row = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));

        //特色商品信息
        $table = $this->db->dbprefix("order_goods");
        $goods_info = $this->Ordergoods_model->get_row(array('order_id' => $order_id));
        $order_distribution = $this->Orderdistribution_model->get_row(array('order_id' => $order_id));
        $data = array(
            'contact_info' => $contact_row,
            'order_info' => $order_row,
            'goods_info' => $goods_info,
            'distribution_info' => $order_distribution
        );
        $data['extra'] = array(array('nav' => array('left' => 'order', 'order' => 'goods')));
        $data['params'] = array(
            'seo' => array('title' => $data['row']['goods_name'], 'keywords' => $data['row']['goods_name'], 'desc' => $data['row']['goods_name'] . '，' . SITECITYNAME . '旅游网为您提供' . $data['row']['goods_name'] . '，价格优惠。' . SITECITYNAME . '旅游网'),
            'nav' => array('current' => 'center'),
        );
        $data['ajax_order_status_url'] = site_url('order/index/ajax_order_status?order_id=' . $order_id);
        $this->load->view(TEMPLATE_NAME . '/goods_info', $data);
    }

    /**
     * 修改订单状态
     */
    public function ajax_order_status()
    {
        $order_id = $this->input->get('order_id');
        $order_row = $this->Orderinfo_model->get_row(" order_id = $order_id ", 'order_type, api_order_sn');
        $data = array('order_id' => $order_id, 'order_status' => 3, 'user_type' => 2);//print_r($data);die('hdk');
        $res = $this->Orderinfo_model->translate_order_status($data);
        $url_type = array(  '1'=>'line_info',
                            '2'=>'ticket_info',
                            '3'=>'goods_info',//shangpin
                            '4'=>'flight_info',//jipiao
                            '5'=>'rent_info'//zuche
                            );
        if ($res)
        {
            if ($order_row['order_type'] == '1' && $order_row['api_order_sn'] > 0)
            {
                $this->Java_Api_model->cancel_order_by_tourist($order_row['api_order_sn'], '取消订单', $this->session->userdata('mobile'), $order_id);
            }

            $this->success('该订单取消成功,插入日志成功!', site_url('order/index/' . $url_type[$res] . '?id=' . $order_id), true);
        }
        else
        {
            $this->error('该订单取消成功,插入日志失败!', site_url('order/index/' . $url_type[$res] . '?id=' . $order_id), true);
        }
    }

    /**
     * ajax线路产品添加到购物车中
     */
    public function ajax_line_cart($fn = 0)
    {
        //判断是否登陆
        $user_id = $this->session->userdata('user_id');

        //线路数据获取
        $arg_post = $this->input->post();

        if ($user_id < 1) {
            $url = site_url('denglu?referer=' . urlencode(site_url("xianlu/{$arg_post['line_id']}.html")));
            $this->success('请登陆用户!', $url, true);
        }

        if ($fn == 1) {
            $this->session->set_userdata('line_cart', $arg_post);
        } else {
            //订单基本信息
            $data = $this->get_order_info($arg_post);
            $this->session->set_userdata('line_cart', $data);
        }


        $this->success('线路信息保存成功', site_url('order/index/line_cart/' . $fn), true);
    }

    /**
     * 线路购物车页面
     */
    public function line_cart($fn = 0)
    {
        $data = array();
        $line_list = $this->session->userdata('line_cart');
        $zhanzuo = $this->Line_go_model->get_zhanzuo($line_list['go_info']['go_id']);
        $go_info = $this->Line_go_model->get_row(array('go_id'=>$line_list['go_info']['go_id']),'seat_num');
        $res_num = $go_info['seat_num']-$zhanzuo;
        $data['res_num'] = $res_num;


        $view = TEMPLATE_NAME . '/line_cart';
        $data['next_url'] = site_url('order/index/line_save');
        $data['val'] = $line_list;
        if ($fn == 1) {
            $view = 'default/line_cart_fx';
            $line = $this->Line_model->get_line_row(array('line_id' => $line_list['line_id']), 'line_id,sid,line_name,line_number,province,city,check');
            $go = $this->Line_go_model->get_row(array('go_id' => $line_list['go_id']), 'th_price,cth_price,go_date');
            $go_price = $this->Line_fx_go_price_model->get_row(array('go_id' => $line_list['go_id'], 'sid' => $this->sid), 'ws_price,cws_price');
            $data['line'] = $line;
            $data['go'] = $go;
            $data['go_price'] = $go_price;
            $departure_city = trim($this->area_model->get_area_name(array($line['province'], $line['city'])), '');
            $data['departure_city'] = str_replace(array('[', ']'), '', $departure_city);
            $data['total_price'] = $line_list['num'] * $go_price['ws_price'] + $line_list['cnum'] * $go_price['cws_price'];
            $data['next_url'] = site_url('order/index/line_save_fx');
        }


        $this->load->view($view, $data);
    }

    /**
     * 线路订单各个部分保存
     */
    public function line_save()
    {
        $arg_post = $this->input->post();

        $user_id = $this->session->userdata('user_id');
        $act = $arg_post['act'];
        if ($act == 'step_1')
        {//填写订单保存
            if (!empty($arg_post['price_num']))
            {
                foreach ($arg_post['price_num'] as $key => $val)
                {
                    $arg_post['price_list'][$key] = array(
                        'price_id' => $arg_post['price_id'][$key],
                        'num' => $val,
                    );
                }
            }
            //订单基本信息
            $data = $this->get_order_info($arg_post);

            print_r($data);
            exit;
            $this->session->set_userdata('line_cart', $data);
            $this->success('填写订单完毕，请核对订单', site_url('order/index/line_cart_step_2'), true);
        }
        elseif ($act == 'step_2')
        {//提交订单

            //获取订单信息
            $order = $this->session->userdata('line_cart');




            if (isset($order['single_room_type']) && $order['single_room_type'] != 1)
            {//是否存在单房差
                $order['total_price'] = $order['total_price'] + $order['go_info']['dif_price'];
            }


            // 判断分销线路余座
            if($order['line_info']['oid']  > 0)
            {
                // java 价格体系
                $price_java_api_data = $this->Line_go_price_model->get_list("go_id = {$order['go_info']['go_id']}", '*');

                $api_total_tourist_num = 0 ;
                if(!empty($order['price_list']))
                {
                    foreach($order['price_list'] as $index => $item)
                    {

                        $api_total_tourist_num += $price_java_api_data[$index]['seat'] * $item['num'];
                    }
                }

                $remain_seat = $this->Java_Api_model->locate_remain_seat($order['line_info']['line_id'], $order['line_info']['oid'], $order['go_info']['go_date']);
                /*if(empty($remain_seat))
                {
                    $this->error('B端接口反馈余座信息异常!', site_url('order/index/line_cart_step_2'), true);
                }*/
                if($api_total_tourist_num > $remain_seat)
                {
                    $this->error('没有足够的余座!', site_url('order/index/line_cart_step_2'), true);
                }
            }

            //订单信息
            $order_data = array(
                'sid' => $this->sid,
                'order_sn' => $this->Orderinfo_model->set_order_sn(),
                'order_type' => 1,//线路订单
                'cat_type' => 2,//线上订单
                'order_status' => 1,//未审核
                'pay_status' => 1,//未支付
                'add_time' => date('Y-m-d H:i:s', now()),
                'paid_account' => '0.00',//已支付金额
                'order_account' => $order['total_price'],//订单总金额
                'order_note' => $order['contact_info']['order_note'],//订单备注
                'user_id' => $user_id,//用户ID
                'admin_user_id' => '0',//管理员ID
            );
            if ($GLOBALS['config']['order_check'] == 0 || $order['line_info']['oid']  > 0) //分销线路订单C端无需审核
            {
                $order_data['order_status'] = 2;
            }



            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if ($order_id)
            {
                //联系人信息保存
                $contact_data = array(
                    'order_id' => $order_id,
                    'name' => $order['contact_info']['name'],
                    'email' => $order['contact_info']['email'],
                    'phone_num' => $order['contact_info']['phone_num'],
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);


                //游客信息保存
                $total_num_ch = 0;
                $tourist_data = array();
                if (isset($order['tourist_data']))
                {
                    foreach ($order['tourist_data'] as $key => $val)
                    {
                        $row = array(
                            'order_id' => $order_id,
                            'name' => $val['name'],
                            'phone_num' => $val['phone_num'],
                            'id_card_type' => $val['id_card_type'],
                            'id_number' => $val['id_number'],
                        );
                        if ($val['cate_attr'] == 2)
                        {
                            $total_num_ch += $val['num'];
                        }
                        $tourist_data[] = $row;
                    }
                    $tourist_return_status = $this->Ordertourist_model->add_batch($tourist_data);

                    //订单团期表保存
                    $go_date = array(
                        'sid' => $this->sid,
                        'order_id' => $order_id,
                        'go_id' => $order['go_info']['go_id'],//团期ID
                        'go_date' => $order['go_info']['go_date'],//发团日期
                        'line_id' => $order['line_info']['line_id'],//线路ID
                        'line_name' => $order['line_info']['line_name'],//线路名称
                        'line_number' => $order['line_info']['line_number'],//线路编号
                        'departure_city' => $order['line_info']['departure_city'],//出发城市
                        'total_num' => count($order['tourist_data']),//总人数
                        'total_num_ch' => $total_num_ch,
                        'single_room_type' => $order['single_room_type'],//选择类型
                        'dif_price' => $order['go_info']['dif_price'],//单房差价格
                        'collection_site' => '',//集合地点
                        'financial_account' => $order['financial_account'] + $order['go_info']['dif_price'],
                        'financial_status' => 1,
                    );
                    $go_date_return_status = $this->Ordergodate_model->add_row($go_date);

                    //修改该线路的总的报名人数
                    $this->Line_model->update_field_num('total_enroll_num', $go_date['total_num'], $go_date['line_id']);

                    //订单团期价格
                    $price_data = array();
                    if (isset($order['price_list']))
                    {
                        foreach ($order['price_list'] as $val)
                        {
                            $row = $val;
                            $row['order_id'] = $order_id;
                            $price_data[] = $row;
                        }
                    }
                    $price_data_return_status = $this->Ordergodateprice_model->add_batch($price_data);
                    if ($contact_return_status && $go_date_return_status && $price_data_return_status)
                    {
                        //调用java接口
                        if ($order['line_info']['oid']  > 0)
                        {
                            $area_data = $this->area_model->get_row(array('id'=>$order['line_info']['city']));
                            $order['line_info']['city_code'] = $area_data['poi_code'];

                            $extend_data = array(
                                'user_id' => $user_id ,
                                'user_name' => $this->session->userdata('mobile')
                            );
                            $request_return = $this->Java_Api_model->pc_interface($order, $price_java_api_data, $extend_data);
                            $api_return = $this->Java_Api_model->create_order_response_logs($request_return, $order_id);
                            if ( ! empty($api_return))
                            {
                                $data['api_order_sn'] = $api_return['orderNumber'];
                                $data['order_status'] = 2;
                                if($api_return['status']) $data['order_status'] = 1;
                                $this->Orderinfo_model->edit_row($data, array('order_id' => $order_id));
                            }
                        }

                         //删除线路订单
                        $this->session->unset_userdata('line_cart');

                        //发送短信
                        $line_order_template = $GLOBALS['config']['line_order_template_submit'];
                        $admin_template = $GLOBALS['config']['sms_template_order'];

                        //短信内容
                        $content = str_replace(array('{$order_sn}', '{$line_name}', '{$go_date}', '{$contact_name}', '{$contact_phone}', '{$order_account}'), array(
                            $order_data['order_sn'],
                            $go_date['line_name'], $go_date['go_date'], $contact_data['name'], $contact_data['phone_num'], $order_data['order_account']
                        ), $line_order_template);

                        $admin_content = str_replace(array('{$order_sn}', '{$line_name}', '{$go_date}', '{$contact_name}', '{$contact_phone}', '{$order_account}'), array(
                            $order_data['order_sn'],
                            $go_date['line_name'], $go_date['go_date'], $contact_data['name'], $contact_data['phone_num'], $order_data['order_account']
                        ), $admin_template);

                        $this->User_model->_send_more_sms($GLOBALS['config']['notify_num'], $admin_content);
                        $this->User_model->_send_sms($contact_data['phone_num'], $content);
                        $this->success('订单提交成功,请等待审核订单并完成支付!', site_url('order/index/line_cart_step_3'), true);
                    }
                    else
                    {
                        $this->error('订单生成失败!', site_url('order/index/line_cart'), true);
                    }
                }
            }
        }
    }


    public function line_save_fx()
    {
        $arg_post = $this->input->post();
        $data = $this->session->userdata('line_cart');

        $user_id = $this->session->userdata('user_id');
        $act = $arg_post['act'];
        if ($act == 'step_1') {//填写订单保存
            $data['contact_phone_num'] = $arg_post['contact_phone_num'];
            $data['contact_name'] = $arg_post['contact_name'];
            $data['contact_email'] = $arg_post['contact_email'];
            $data['order_note'] = $arg_post['order_note'];

            foreach ($arg_post['name'] as $key => $val) {
                $data['tourist_data'][$key]['name'] = $val;
                $data['tourist_data'][$key]['id_card_type'] = $arg_post['id_card_type'][$key];
                $data['tourist_data'][$key]['id_number'] = $arg_post['id_number'][$key];
                $data['tourist_data'][$key]['phone_num'] = $arg_post['phone_num'][$key];
            }

            //订单基本信息
            // $data = $this->get_order_info($arg_post);
            $this->session->set_userdata('line_cart', $data);
            $this->success('填写订单完毕，请核对订单', site_url('order/index/line_cart_step_2_fx'), true);
        } elseif ($act == 'step_2') {//提交订单

            //获取订单信息
            $order = $this->session->userdata('line_cart');

            $line = $this->Line_model->get_line_row(array('line_id' => $order['line_id']), 'line_id,sid,line_name,line_number,province,city,check');
            $go = $this->Line_go_model->get_row(array('go_id' => $order['go_id']), 'th_price,cth_price,go_date');
            $go_price = $this->Line_fx_go_price_model->get_row(array('go_id' => $order['go_id'], 'sid' => $this->sid), 'ws_price,cws_price');

            $departure_city = trim($this->area_model->get_area_name(array($line['province'], $line['city'])), '');
            $departure_city = str_replace(array('[', ']'), '', $departure_city);
            $total_price = $order['num'] * $go_price['ws_price'] + $order['cnum'] * $go_price['cws_price'];


            //订单信息
            $order_data = array(
                'sid' => $this->sid,
                'order_sn' => $this->Orderinfo_model->set_order_sn(),
                'order_type' => 1,//线路订单
                'cat_type' => 2,//线上订单
                'order_status' => $line['check'] ? 1 : 2,//未审核
                'pay_status' => 1,//未支付
                'add_time' => date('Y-m-d H:i:s', now()),
                'paid_account' => 0,//已支付金额
                'order_account' => $total_price,//订单总金额
                'order_note' => $order['order_note'],//订单备注
                'user_id' => $user_id,//用户ID
                'admin_user_id' => '0',//管理员ID
                'from_sid' => $line['sid']
            );

            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if ($order_id) {
                //联系人信息保存
                $contact_data = array(
                    'order_id' => $order_id,
                    'name' => $order['contact_name'],
                    'email' => $order['contact_email'],
                    'phone_num' => $order['contact_phone_num'],
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

                //游客信息保存
                $tourist_data = array();
                if (isset($order['tourist_data'])) {
                    foreach ($order['tourist_data'] as $key => $val) {
                        $row = array(
                            'order_id' => $order_id,
                            'name' => $val['name'],
                            'phone_num' => $val['phone_num'],
                            'id_card_type' => $val['id_card_type'],
                            'id_number' => $val['id_number'],
                        );
                        $tourist_data[] = $row;
                    }
                    $tourist_return_status = $this->Ordertourist_model->add_batch($tourist_data);
                }


                //订单团期表保存

                $go_date = array(
                    'sid' => $this->sid,
                    'from_sid' => $line['sid'],
                    'order_id' => $order_id,
                    'go_id' => $order['go_id'],//团期ID
                    'go_date' => $go['go_date'],//发团日期
                    'line_id' => $order['line_id'],//线路ID
                    'line_name' => $line['line_name'],//线路名称
                    'line_number' => $line['line_number'],//线路编码
                    'departure_city' => $departure_city,//出发城市
                    'total_num' => $order['num'] + $order['cnum'],//总人数
                    'single_room_type' => isset($arg_post['single_room_type']) ? $arg_post['single_room_type'] : '1',//选择类型
                    'collection_site' => '',//集合地点
                    'financial_account' => $go['th_price'] * $order['num'] + $go['cth_price'] * $order['cnum'],//财务支付金额
                    'financial_status' => 1,//财务付款状态 未支付
                    'th_price' => $go['th_price'],
                    'cth_price' => $go['cth_price'],
                    'ws_price' => $go_price['ws_price'],
                    'cws_price' => $go_price['cws_price'],
                    'num' => $order['num'],
                    'cnum' => $order['cnum']
                );

                $go_date_return_status = $this->Ordergodate_model->add_row($go_date);

                //修改该线路的总的报名人数
                $this->Line_model->update_field_num('total_enroll_num', $go_date['total_num'], $go_date['line_id']);


                if ($contact_return_status && $go_date_return_status) {




                    //删除线路订单
                    $this->session->unset_userdata('line_cart');

                    //发送短信
                    $line_order_template = $GLOBALS['config']['line_order_template_submit'];
                    $admin_template = $GLOBALS['config']['sms_template_order'];

                    //短信内容
                    $content = str_replace(array('{$order_sn}', '{$line_name}', '{$go_date}', '{$contact_name}', '{$contact_phone}', '{$order_account}'), array($order_data['order_sn'],
                        $go_date['line_name'], $go_date['go_date'], $contact_data['name'], $contact_data['phone_num'], $order_data['order_account']), $line_order_template);

                    $admin_content = str_replace(array('{$order_sn}', '{$line_name}', '{$go_date}', '{$contact_name}', '{$contact_phone}', '{$order_account}'), array($order_data['order_sn'],
                        $go_date['line_name'], $go_date['go_date'], $contact_data['name'], $contact_data['phone_num'], $order_data['order_account']), $admin_template);

                    $this->User_model->_send_more_sms($GLOBALS['config']['notify_num'], $admin_content);
                    $this->User_model->_send_sms($contact_data['phone_num'], $content);
                    $this->success('订单提交成功,请等待审核订单并完成支付!', site_url('order/index/line_cart_step_3'), true);
                } else {
                    $this->error('订单生成失败!', site_url('order/index/line_cart/1'), true);
                }
            }
        }
    }


    /**
     * 根据传值获取订单基本信息
     * @param $arg_post
     */
    public function get_order_info($arg_post)
    {

        //线路信息
        $line = $this->Line_model->get_row(array('line_id' => $arg_post['line_id']));

        //出发省份城市
        $departure_city = trim($this->area_model->get_area_name(array($line['province'], $line['city'])), '');
        $departure_city = str_replace(array('[', ']'), '', $departure_city);
        $line['departure_city'] = $departure_city;

        //团期信息
        $go_info = $this->Line_go_model->get_row(array('go_id' => $arg_post['go_id']), 'go_id,go_date,dif_price');

        //价格类型
        $price_list = array();
        $total_th_price = 0;
        $total_price = 0;
        $total_num = 0;
        if (!empty($arg_post['price_list'])) {
            foreach ($arg_post['price_list'] as $key => $val) {
                $price_info = $this->Line_go_price_model->get_row(array('price_id' => $val['price_id']));
                $val['price_name'] = $price_info['price_name'];
                $val['price'] = $price_info['ws_price'];
                $val['th_price'] = $price_info['th_price'];
                $price_list[] = $val;
                $total_price += $val['price'] * $val['num'];
                $total_th_price += $val['th_price'] * $val['num'];
                $total_num += $val['num'];
            }
        }

        //联系人信息
        $contact_data = array();
        if (isset($arg_post['contact_name'])) {
            $contact_data = array(
                'name' => $arg_post['contact_name'],
                'email' => $arg_post['contact_email'],
                'phone_num' => $arg_post['contact_phone_num'],
                'order_note' => $arg_post['order_note'],
            );
        }

        //单房差信息
        $single_room_type = '1';
        if (isset($arg_post['single_room_type'])) {
            $single_room_type = $arg_post['single_room_type'];
        }

        //游客信息
        $tourist_data = array();
        if (isset($arg_post['name'])) {
            foreach ($arg_post['name'] as $key => $val) {
                $row = array(
                    'name' => $val,
                    'phone_num' => $arg_post['phone_num'][$key],
                    'id_card_type' => $arg_post['id_card_type'][$key],
                    'id_number' => $arg_post['id_number'][$key],
                );
                $tourist_data[] = $row;
            }
        }

        $data = array(
            'line_info' => $line,
            'go_info' => $go_info,
            'price_list' => $price_list,
            'contact_info' => $contact_data,
            'single_room_type' => $single_room_type,
            'tourist_data' => $tourist_data,
            'total_price' => $total_price,
            'total_num' => $total_num,
            'financial_account' => $total_th_price,
        );
        return $data;
    }

    /**
     * 线路订单核对页面
     */
    public function line_cart_step_2()
    {
        $data = array();
        $line_list = $this->session->userdata('line_cart');
        $data['val'] = $line_list;
        $data['next_url'] = site_url('order/index/line_save');
        $data['card_type'] = array('1'=>'身份证','2'=>'护照','3'=>'军官证');
        $this->load->view(TEMPLATE_NAME . '/line_cart_step_2', $data);
    }


    public function line_cart_step_2_fx()
    {
        $data = array();
        $line_list = $this->session->userdata('line_cart');
        $line = $this->Line_model->get_line_row(array('line_id' => $line_list['line_id']), 'line_id,sid,line_name,line_number,province,city,check');
        $go = $this->Line_go_model->get_row(array('go_id' => $line_list['go_id']), 'th_price,cth_price,go_date');
        $go_price = $this->Line_fx_go_price_model->get_row(array('go_id' => $line_list['go_id'], 'sid' => $this->sid), 'ws_price,cws_price');
        $data['line'] = $line;
        $data['go'] = $go;
        $data['go_price'] = $go_price;
        $departure_city = trim($this->area_model->get_area_name(array($line['province'], $line['city'])), '');
        $data['departure_city'] = str_replace(array('[', ']'), '', $departure_city);
        $data['total_price'] = $line_list['num'] * $go_price['ws_price'] + $line_list['cnum'] * $go_price['cws_price'];
        $data['val'] = $line_list;
        $data['next_url'] = site_url('order/index/line_save_fx');
        $this->load->view('default/line_cart_step_2_fx', $data);
    }

    /**
     * 线路订单提交订单完成页面
     */
    public function line_cart_step_3()
    {
        $data['sitename'] = $GLOBALS['config']['sitename'];
        $data['site_phone'] = $GLOBALS['config']['site_service_phone'];
        $data['site_service_time'] = $GLOBALS['config']['site_service_time'];
        $data['site_record_num'] = $GLOBALS['config']['site_record_num'];
        $this->load->view(TEMPLATE_NAME . '/line_cart_step_3', $data);
    }


    /**
     * 订单数据更新
     */

    public function update_flight_info($order_id)
    {
        $return = array();
        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id));
        // 接口订单信息
        $server_data['order_sn'] = $order_row['order_sn'];
        $remote_info = $this->flight_model->_order_detail($server_data);

        if ($remote_info == '300208') {
            $this->Orderinfo_model->translate_order_status(array('order_id' => $order_id, 'order_status' => '3', 'note' => '取消'));
        }
        if (is_array($remote_info) && isset($remote_info['Response']) && !empty($remote_info['Response'])) {
            $return = $remote_info['Response'];
            $return['status_msg'] = $this->flight_model->return_status($remote_info['Response']['Status']);
            $this->Orderflight_model->edit_row(array('status' => $remote_info['Response']['Status']), array('order_id' => $order_id));
            if ($remote_info['Response']['Status'] == '2' && $order_row['order_status'] == '2') {

                $this->Orderflight_model->edit_row(array('ticket_no' => $remote_info['Response']['TicketNo']), array('order_id' => $order_id));

                $pay_data = array(
                    'order_id' => $order_id,
                    'order_status' => '4',
                    'user_type' => '2',
                );

                $this->Orderinfo_model->translate_order_status($pay_data);

                $log_data = array(
                    'pay_account' => $remote_info['Response']['PayMoney'],
                    'order_id' => $order_id,
                    'order_status ' => '4',
                    'user_type' => '2',
                    'status' => '2',
                    'type' => '1'
                );

                $this->Orderpaylog_model->save_pay_account($log_data);
            }

            return $return;
        }

        return false;
    }


    /**
     * 租车订单列表
     */
    public function rent_list()
    {
        $order_info_table = $this->db->dbprefix("order_info");
        $where = " $order_info_table.user_id=".$this->session->userdata('user_id').' AND ' . $order_info_table . ".order_type=5 AND " . $order_info_table . ".sid= ".SITEID . ' ';//门票订单
        if($_GET['pay']==3){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status=3 ";
        }else if($_GET['pay']==1){
            if ($where != '') $where .= " AND ";
            $where .= "" . $order_info_table . ".pay_status!=3 ";
        }

        $page_config['perpage'] = 20;   //每页条数
        $page_config['part'] = 2;//当前页前后链接数量
        $page_config['url'] = 'order/index/rent_list';//url
        $page_config['param'] = '?pay='.$_GET['pay'];//参数
        $page_config['seg'] = 4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']) : 1;//当前页
        $page_config['total'] = $this->Orderinfo_model->get_rent_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $this->data['list'] = $this->Orderinfo_model->get_index_rent_list($page_config['perpage'], $page_config['nowindex'], $where);

        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['car_type_name'] = $this->rent_model->return_car_type($v['car_type']);
                $this->data['list'][$k]['service_type_name'] = $this->rent_model->return_service_type($v['cate_attr']);
                $this->data['list'][$k]['edit_url']= $this->data['list'][$k]['detail_url'] = site_url('order/index/rent_info?id=' . $v['order_id']);
            }
        }

        $this->data['extra'] = array(array('nav' => array('left' => 'order', 'order' => 'rent')));
        $this->data['params'] = array(
            'seo' => array('', 'desc' => ''),
            'nav' => array('current' => 'center'),
        );
        $this->load->view(TEMPLATE_NAME . '/rent_list', $this->data);
    }


    /**
     * 租车订单详情
     */
    public function rent_info()
    {
        $order_id = $this->input->get('id');
        //联系人信息
        $this->data['contact_info'] = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));

        //订单基本信息
        $this->data['order_info'] = $this->Orderinfo_model->get_row(array('order_id' => $order_id));
        $this->data['rent_order_info'] = $this->Orderrent_model->get_row(array('order_id' => $order_id));

        $this->data['price_info'] = $this->rent_model->_car_price_info($this->data['rent_order_info']['price_id']);
        $this->data['price_info']['service_type_name'] = $this->rent_model->return_service_type($this->data['price_info']['cate_attr']);
        $this->data['car_info'] = $this->rent_model->_car_info($this->data['price_info']['car_id']);
        $this->data['car_info']['seat_number'] = $this->rent_model->return_seat_number($this->data['car_info']['seat']);
        $this->data['car_info']['car_type_name'] = $this->rent_model->return_car_type($this->data['car_info']['car_type']);

        $this->data['extra'] = array(array('nav' => array('left' => 'order', 'order' => 'rent')));
        $this->data['params'] = array(
            'seo' => array('title' => '', 'desc' => ''),
            'nav' => array('current' => 'center'),
        );
        $this->data['ajax_order_status_url'] = site_url('order/index/ajax_order_status?order_id=' . $order_id);

        $this->load->view(TEMPLATE_NAME . '/rent_info', $this->data);
    }

    /**
     * 特色商品订单页面
     */
    public function goods_order(){
        $good_id = $this->input->get('id');
        if($this->session->userdata('user_id')<1){
            redirect('denglu?referer='.urlencode(site_url('order/index/goods_order/?id='.$good_id)));
        }
        $data = array();
        $row = $this->Goods_model->get_row(array('goods_id'=>$good_id,'status'=>1,'sid'=>$this->sid),'goods_id,goods_name,goods_price,is_shipping');
        if(empty($row))redirect(site_url('goods'));
        $data['row'] = $row;
        $data['user_info'] = $this->User_model->get_row(array('user_id'=>$this->uid),'username,mobile');
        //特色商品 具有配送方式
        //判断商品是否可以配送
        if($row['is_shipping']==1) {
            $data['distribution'] = $this->Orderdistribution_model->get_limit_one(array('uid'=>$this->uid));
        }

        $data['post_url'] = site_url('order/index/save_goods_order');
        $this->load->view(TEMPLATE_NAME.'/goods_order',$data);
    }

    /**
     * 特色商品订单保存操作
     */
    public function save_goods_order(){
        $arg_form = $this->input->post('Form');
        $arg_contact = $this->input->post('contact_info');
        $arg_distribution = $this->input->post('distribution');
        $id = $arg_form['id'];
        $row = $this->Goods_model->get_row(array('goods_id'=>$id),'goods_id,goods_price,goods_name');
        //订单总价
        $total_price = $row['goods_price'] * $arg_form['goods_num'];

        //订单信息
        $order_data = array(
            'sid'=>$this->sid,
            'order_sn' => $this->Orderinfo_model->set_order_sn(),
            'order_type' => 3,//特色商品订单
            'cat_type' => 2,//线上订单
            'order_status' => 2,//已审核
            'pay_status' => 1,//未支付
            'add_time' => date('Y-m-d H:i:s',now()),
            'paid_account' => '0.00',//已支付金额
            'order_account' => $total_price,//订单总金额
            'order_note' => $arg_contact['order_note'],//订单备注
            'user_id' => $this->session->userdata('user_id'),//用户ID
            'admin_user_id' => '0',//管理员ID
            'from_sid'=>$this->sid,
        );
        $order_id = $this->Orderinfo_model->add_row($order_data);
        $this->Goods_model->set_goods_sold_num(array('goods_id'=>$id,'sid'=>$this->sid),$arg_form['goods_num']);//修改已售人数

        if($order_id) {
            //联系人信息保存
            $contact_data = array(
                'order_id' => $order_id,
                'name' => $arg_contact['name'],
                'phone_num' => $arg_contact['phone_num'],
            );
            $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);
        }else{
            $this->error('提交订单失败!',site_url('goods'));
        }

        //订单商品
        $batch_goods = array(array(
            'order_id' => $order_id,
            'goods_id' => $row['goods_id'],
            'price' => $row['goods_price'],
            'num' => $arg_form['goods_num'],
            'goods_name'=>$row['goods_name'],
        ));
        $batch_return_status = $this->Ordergoods_model->add_batch($batch_goods);

        //发送短信
        $goods_order_template = $GLOBALS['config']['goods_order_template_submit'];

        //短信内容
        $content = str_replace(array('{$order_sn}','{$goods_name}','{$num}','{$contact_name}','{$contact_phone}','{$order_account}'),array($order_data['order_sn'],
            $row['goods_name'],$arg_form['goods_num'],$contact_data['name'],$contact_data['phone_num'],$order_data['order_account']),$goods_order_template);
        $this->User_model->_send_sms($contact_data['phone_num'],$content);

        $this->success('订单提交成功,请完成订单支付!',site_url('pay/'.$order_id),true);
        //$this->success('订单提交成功,请等待审核订单并完成支付!',site_url('promotion/order/order_result?id='.$order_id),true);
    }
}